//
//  MLWeiboCell.swift
//  MLSina20160624
//
//  Created by manmanlang on 16/6/28.
//  Copyright © 2016年 manmanlang. All rights reserved.
//

import UIKit
import SnapKit
class MLWeiboCell: UITableViewCell {
    
    //原创微博
    private lazy var weiboOriginalView: MLWeiboCellOriginalView = MLWeiboCellOriginalView()
    //转发微博
    private lazy var weiboRetweetView:MLRetweet = MLRetweet()
    //底部toolbar
    private lazy var weiboToolbar:MLWeiboToolbar = MLWeiboToolbar()
    
    var toolBarTopConstraint:Constraint?//记录约束
    
    var weiboData:MLWeiboData? {
        
        didSet {
            
            weiboOriginalView.weiboData = weiboData
            toolBarTopConstraint?.uninstall()//取消原有的约束
            if weiboData?.retweeted_status == nil {
                //么有转发微博
                weiboRetweetView.hidden = true
                //如果已经取消了约束，那么也可以使用updata
                weiboToolbar.snp_makeConstraints(closure: { (make) -> Void in
                    toolBarTopConstraint = make.top.equalTo(weiboOriginalView.snp_bottom).constraint
                })
                
            }else {
                //有转发微博
                weiboRetweetView.weibo = weiboData
                weiboRetweetView.hidden = false
                
                weiboToolbar.snp_makeConstraints(closure: { (make) -> Void in
                    
                    toolBarTopConstraint = make.top.equalTo(weiboRetweetView.snp_bottom).constraint
                })
            }
//            给toolbar赋值
            weiboToolbar.weibo = weiboData
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
//        self.backgroundColor = UIColor.whiteColor()
        self.contentView.backgroundColor = RBG(237, green: 237, blue: 237)
        
        contentView.addSubview(weiboOriginalView)
        contentView.addSubview(weiboRetweetView)
        contentView.addSubview(weiboToolbar)
        
        weiboOriginalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
        weiboRetweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(weiboOriginalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        weiboToolbar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            toolBarTopConstraint = make.top.equalTo(weiboRetweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(weiboToolbar)
        }
    }
    

}
